﻿namespace CloudSql.Test
{
    using System;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Xml.Linq;
    using CloudSql.Entities;
    using Microsoft.Samples.ServiceHosting.StorageClient;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class DataServiceTests
    {
        public DataServiceTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// This test checks that the AuditRows table is accessible in the dev fabric
        /// </summary>
        [TestMethod]
        public void Check_Access_To_AuditRows_Table_On_Local_Dev_Cloud()
        {
            var storageAccountInfo = new StorageAccountInfo(new Uri("http://127.0.0.1:10002"), true, "devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
            TableStorage.CreateTablesFromModel(typeof(AuditRowEntityDataServiceContext), storageAccountInfo);
            var context = new AuditRowEntityDataServiceContext(storageAccountInfo);
            var auditRows = context.AuditRows.Take(10);
        }

        /// <summary>
        /// This test takes an xml fragment and returns the found row in the current database
        /// </summary>
        [TestMethod]
        public void Generate_Xml_From_Row_Specification()
        {
            var sqlConnection = new SqlConnection(CloudSql.Test.Properties.Settings.Default.Connection);
            sqlConnection.Open();
            XElement rowSpecificationXml = new XElement("rowSpecification", 
                new XAttribute("tableName", "ProductCategory"),
                new XAttribute("tableSchema", "dbo"),
                new XElement("keys",
                    new XElement("key",
                        new XAttribute("fieldName", "productId"),
                        new XAttribute("value", "1")),
                    new XElement("key",
                        new XAttribute("fieldName", "categoryId"),
                        new XAttribute("value", "1"))
                ));
            string rowKey = string.Empty;
            XElement rowAsXml = CloudSql.Client.AuditRowDataSource.RenderRowAsXElement(sqlConnection, rowSpecificationXml.ToString(), ref rowKey);
        }

        /// <summary>
        /// This test adds a row from SQL to the cloud.  Does the same as what the SQL CLR function would do
        /// </summary>
        [TestMethod]
        public void Add_Audit_Row()
        {
            SqlConnection sqlConnection = new SqlConnection(CloudSql.Test.Properties.Settings.Default.Connection);
            sqlConnection.Open();
            XElement rowSpecificationXml = new XElement("rowSpecification",
                new XAttribute("tableName", "ProductCategory"),
                new XAttribute("tableSchema", "dbo"),
                new XElement("keys",
                    new XElement("key",
                        new XAttribute("fieldName", "productId"),
                        new XAttribute("value", "1")),
                    new XElement("key",
                        new XAttribute("fieldName", "categoryId"),
                        new XAttribute("value", "1"))
                ));
            string rowKey = "";
            XElement rowAsXml = CloudSql.Client.AuditRowDataSource.RenderRowAsXElement(sqlConnection, rowSpecificationXml.ToString(), ref rowKey);

            AuditRowService.AuditRowService auditRowService = new CloudSql.Test.AuditRowService.AuditRowService();
            auditRowService.AddAuditRow("P1", rowKey, rowKey, rowAsXml.ToString());
        }

        [TestMethod]
        public void Check_service_url_from_database()
        {
            var sqlConnection = new SqlConnection(CloudSql.Test.Properties.Settings.Default.Connection);
            sqlConnection.Open();
            string url = CloudSql.Client.AuditRowDataSource.GetServiceUrl(sqlConnection);
            Assert.AreEqual(url, "http://auditrows.cloudapp.net/AuditRowService.svc");
        }
    }
}
